/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ClasesKinesiologia;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;


/**
 *
 * @author Adrian
 */
public class Ficheros
{

    File ruta = new File("");
    private String fi;
     public void copyFile(File s, File t)
    {
        try{
              FileChannel in = (new FileInputStream(s)).getChannel();
              FileChannel out = (new FileOutputStream(t)).getChannel();
              in.transferTo(0, s.length(), out);
              in.close();
              out.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
     }
  /*   public boolean existUser()
    {
         File user = new File(ruta.getAbsolutePath()+"\\user");
         if(user.exists()==true) return true;
         return false;

    }

     public void guardaLoc(List<String> loca)
    {
          File med= new File(ruta.getAbsolutePath()+"\\Localidades");
           if(med.exists()==true) med.delete();
         this.guarda(med,loca);
     }


     public void guardaMedicos(List<String> mi_lista)
     {
         File med= new File(ruta.getAbsolutePath()+"\\Medicos");
         if(med.exists()==true) med.delete();
         this.guarda(med,mi_lista);
     }

     public Object cargaMed()
    {
        File med= new File(ruta.getAbsolutePath()+"\\Medicos");
        if(med.exists()==true)
        {
        Object o = this.getObj(med);
        return o;
        }
        return null;
     }

     public void guardaUser(usuario user)
     {
         File us = new File(ruta.getAbsolutePath()+"\\user");
         if(us.exists()==true) us.delete();
         Object obj = user;
         guarda(us,obj);
     }

      public boolean validaUser(String usact,String contraseña)
     {
         File us = new File(ruta.getAbsolutePath()+"\\user");
         Object u = this.getObj(us);
         if(us.exists()==true)
         {
             usuario User = (usuario) this.getObj(us);
             return User.validauser(usact, contraseña);
         }
         return false;
     }*/


     public boolean obs(String nom, Ficha fi)
    {
         File fic = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom+"\\"+fi.getnom()+"\\observaciones.doc");
        // System.out.print( fic.exists());
         return fic.exists();
     }
     public void verObs(String nom, Ficha fi)
    {
          File fic = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom+"\\"+fi.getnom()+"\\observaciones.doc");
          if(fic.exists()!=true)
          {
             BufferedWriter outfile;
            try {
                 outfile = new BufferedWriter(new FileWriter(fic));
                 String str="   ";
                 outfile.write(str);
        outfile.close();
            } catch (IOException ex) {
                Logger.getLogger(Ficheros.class.getName()).log(Level.SEVERE, null, ex);
            }

          }
           Runtime r = Runtime.getRuntime();
        try {
            r.exec("explorer.exe "+fic.getCanonicalPath());
          } catch (IOException ex) {

          }
    }
/*


     public void guarda(File f,Object o)
    {
          ObjectOutputStream oos = null;
          try {

             oos = new ObjectOutputStream(new FileOutputStream(f));
             oos.writeObject(o);


            } catch (IOException ex) {
               JOptionPane.showMessageDialog(null, "error al guardar");
            }

         finally {
            try {
                oos.close();
            } catch (IOException ex) {
               JOptionPane.showMessageDialog(null, "error al guardar");
            }
        }
    }

     public int cargaSeciones(Ficha f,List<Seciones> s)
    {
         int ult = 0;
         File sec = new File(f.getUbi()+"\\Seciones");
         for(String g:sec.list())
         {
             File aux = new File(sec.getAbsolutePath()+"\\"+g);
             Object o = getObj(aux);
             Seciones ss = (Seciones) o;
             s.add(ss);
             ult = Integer.valueOf(g);
         }
         return ult;
    }
*/
     public File agregaFormu(Paciente p,File f)
    {
        File form = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+p.getDoc()+"\\Formularios\\"+f.getName());
        this.copyFile(f, form);
        return form;
     }


 /*    public Object getObj(File lee)
    {
         Object o = null;
         ObjectInputStream ois = null;

          try {
                ois = new ObjectInputStream(new FileInputStream(lee));
            try {
                o = ois.readObject();
                ois.close();
               // System.out.print(("paciente cargado\n"));



            } catch (ClassNotFoundException ex) {
               JOptionPane.showMessageDialog(null, "error getobj");
            }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "error getobj");
            }
        return o;
    }

     public void guardaSec(Seciones s,Ficha f,int nom)
    {
       File sec = new File(f.getUbi()+"\\Seciones\\"+String.valueOf(nom));
       if(sec.exists()==true) sec.delete();
       s.setNom(nom);
       Object j = s;
       guarda(sec, j);

    }*/
     
     public void cargaFormu(DefaultListModel d,String nom,String fi)
     {
        
        File fic = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom+"\\Formularios");
        if(fic.exists()==false) fic.mkdir();
        for(String g:fic.list())
        {
            d.addElement(g);
        }

     }
     /*
     public Ficha getFicha(String nom,String fi)
     {
         File fic = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom+"\\"+fi+"\\propiedades");
         Ficha aux= null;
         Object au = this.getObj(fic);
         aux = (Ficha) au;
         aux.setUbi(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom+"\\"+fi);
         return aux;
         
     }

     public void borraFicha(Ficha f,String nomp)
    {
       File Dpac = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nomp+"\\"+f.getnom());
       borrarDirectorio(Dpac);
       Dpac.delete();
   }
     
     public void borrarDirectorio (File directorio){

                File[] ficheros = directorio.listFiles();

                for (int x=0;x<ficheros.length;x++){
                        if (ficheros[x].isDirectory()) 
                        {

                                borrarDirectorio(ficheros[x]);
                        }
                       ficheros[x].delete();
                }
     }
     
     public String guardaFicha(Ficha f,String nom)
     {
         File ficha = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom);
         if(ficha.exists()==false) ficha.mkdir();
         File Dpac = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom+"\\"+f.getnom());
         if(Dpac.exists()==false) Dpac.mkdir();
        // System.out.print("\n"+Dpac.exists()+" nf "+f.getnom()+ "\n");
         File fic = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom+"\\"+f.getnom()+"\\propiedades");
         //System.out.print(fic.exists()+"\n");
         if(fic.exists()==true) fic.delete();
         File sec = new File(Dpac.getAbsolutePath()+"\\Seciones");
         if(sec.exists()==false) sec.mkdir();
         Object o = f;
         guarda(fic, o);
         return Dpac.getAbsolutePath();
    }

     
     public void cargaFicheros(DefaultListModel f,String nom)
     {
         File Dpac = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nom);
         if(Dpac.exists()==false) Dpac.mkdir();
         for(String g:Dpac.list())
         {
             if(g.equals("Formularios")!=true && g.equals("Seciones")!= true) f.addElement(g);
         }
     }

    public void guardaOS(List<String> os)
     {
         File obra = new File(ruta.getAbsolutePath()+"\\obrasSociales");
         if(obra.exists()==true)
         {
             obra.delete();
         }
         Object o = os;
         this.guarda(obra, o);
     }

    public void actualiza(List<Paciente> pac)
    {
        File pa = new File(ruta.getAbsolutePath() + "\\Pac");
        pa.mkdir();

        for(Paciente p:pac)
        {
            String nomp =p.getDoc();
            File auxp = new File(ruta.getAbsolutePath()+"\\Pacientes\\"+nomp);
            auxp.delete();
            this.guardaPac(p);
            File auxdf = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nomp);

        }
    }

    public void borraPac(Paciente p)
    {
        String nomp = p.getDoc();
        File auxp = new File(ruta.getAbsolutePath()+"\\Pacientes\\"+nomp);
        auxp.delete();
        File auxdf = new File(ruta.getAbsolutePath()+"\\DatosPersonales\\"+nomp);
        auxdf.delete();
    }

     public Paciente cargaPac(File p)
    {
        Paciente aux = null;
        FileReader fr = null;
        File pa = new File(ruta.getAbsolutePath() + "\\Pac");
        

          Object au = this.getObj(p);
          aux = (Paciente) au;
        return aux;
    }


    public void guardaPac(Paciente p)
    {

            File ruta = new File("");
            //parte de pacientes
            File pac= new File(ruta.getAbsolutePath()+"\\Pacientes");
            File act = new File(pac.getAbsolutePath()+"\\"+p.getDoc());
            if(act.exists()==true)
            {
            act.delete();
            }
           
           Object o = p;
           guarda(act, o);

    }*/
}
